package cn.e3mall.content.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.e3mall.common.E3Result;
import cn.e3mall.common.EasyUIDataGridResult;
import cn.e3mall.common.JsonUtils;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.jedis.JedisClient;
import cn.e3mall.jedis.JedisClientPool;
import cn.e3mall.mapper.TbContentMapper;
import cn.e3mall.pojo.TbContent;
import cn.e3mall.pojo.TbContentExample;
import cn.e3mall.pojo.TbContentExample.Criteria;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbItemExample;

@Service
public class ContentServiceImpl implements ContentService {
	
	@Autowired
	private TbContentMapper tbContentMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${CONTENT_KEY}")
	private String CONTENT_KEY;

	@Override
	public TbContent getEntity(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public E3Result addEntity(Object T) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public E3Result updateEntity(Object T) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public E3Result deleteEntityByPk(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public E3Result addEntity(TbContent tbContent) {
		//补全属性
		tbContent.setCreated(new Date());
		tbContent.setUpdated(new Date());
				
		tbContentMapper.insert(tbContent);
		//缓存同步
		jedisClient.hdel(CONTENT_KEY, tbContent.getCategoryId().toString());
		return E3Result.ok();
	}

	@Override
	public EasyUIDataGridResult selectContentList(int page, int rows, Long categoryId) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		//执行查询
		List<TbContent> tbContentList = tbContentMapper.selectByCategoryId(categoryId);
		//取分页信息
		PageInfo<TbContent> pageInfo = new PageInfo<>(tbContentList);
		
		//创建返回结果对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setTotal(new Long(pageInfo.getTotal()).intValue());
		result.setRows(tbContentList);
		
		return result;
	}

	@Override
	public E3Result deleteByPks(Long[] ids) {
		tbContentMapper.deleteByPks(ids);
		return E3Result.ok();
	}

	@Override
	public E3Result updateContent(TbContent tbContent) {
		tbContentMapper.updateByPrimaryKeyWithBLOBs(tbContent);
		return E3Result.ok();
	}

	@Override
	public List<TbContent> getContentList(Long cid) {
		//查询缓存
		try {
			String json = jedisClient.hget(CONTENT_KEY, cid + "");
			//判断json是否为空
			if (StringUtils.isNotBlank(json)) {
				//把json转换成list
				List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TbContentExample tbContentExample = new TbContentExample();
		Criteria criteria = tbContentExample.createCriteria();
		criteria.andCategoryIdEqualTo(cid);
		List<TbContent> tbContentList = tbContentMapper.selectByExample(tbContentExample);
		//向缓存中添加数据
		try {
			jedisClient.hset(CONTENT_KEY, cid + "", JsonUtils.objectToJson(tbContentList));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tbContentList;
	}

}
